﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Electric;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using libxl;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class EquipmentListForm : System.Windows.Forms.Form
	{
		public EquipmentListForm(UIDocument uiDoc, ExternalEvent exEvent, RequestHandler handler)
		{
			this.InitializeComponent();
			this.m_uiDoc = uiDoc;
			this.m_doc = uiDoc.ActiveView.Document;
			this.m_Handler = handler;
			this.m_ExEvent = exEvent;
		}

		public EquipmentFamily SelectedFamily
		{
			get
			{
				return this.m_selectedFamily;
			}
		}

		public EquipmentListData SelectedData
		{
			get
			{
				if (this.m_selectedGridRow == null || this.m_selectedGridRow.Tag == null)
				{
					return null;
				}
				return this.m_selectedGridRow.Tag as EquipmentListData;
			}
		}

		public int FiWidth
		{
			get
			{
				return Convert.ToInt32(this.txtWidth.Text.Trim());
			}
		}

		public int FiHeight
		{
			get
			{
				return Convert.ToInt32(this.txtHeight.Text.Trim());
			}
		}

		public int FiDepth
		{
			get
			{
				return Convert.ToInt32(this.txtDepth.Text.Trim());
			}
		}

		public bool IsAssignLevel
		{
			get
			{
				return this.checkBoxIsAssignLevel.Checked;
			}
		}

		public void RefreshSelectedGridRowState()
		{
			if (this.m_selectedGridRow == null || this.m_selectedGridRow.Tag == null)
			{
				return;
			}
			EquipmentListData equipmentListData = (EquipmentListData)this.m_selectedGridRow.Tag;
			if (equipmentListData.IsChecked)
			{
				return;
			}
			ICollection<Element> collection = CableLayRevitCommon.CollectElectricalEquipment(this.m_doc);
			if (collection == null || collection.Count == 0)
			{
				return;
			}
			foreach (Element element in collection)
			{
				Parameter parameter = element.GetParameter("编号");
				if (parameter != null && !(parameter.AsString() != equipmentListData.Number))
				{
					this.m_selectedGridRow.Cells[0].Value = YArchitech.Electric.Resources.GetImage("SuccessFace16");
					equipmentListData.ObjectId = element.Id;
					equipmentListData.IsChecked = true;
					break;
				}
			}
		}

		private void EquipmentListForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.m_pictureBackColor = this.picture1.BackColor;
			this.InitPictureBox();
			this.InitButtonImage();
			this.ReadFormDataFromXml();
			this.RefreshPictureBackColor();
			this.LoadDataFromDb();
		}

		private void InitPictureBox()
		{
			string text = Path.Combine(Product.FamilyImageLocation, "CableLay", "HY-SwitchBox.png");
			this.picture1.Image = (File.Exists(text) ? Image.FromFile(text) : this.picture1.InitialImage);
			text = Path.Combine(Product.FamilyImageLocation, "CableLay", "HY-Mechanical.png");
			this.picture2.Image = (File.Exists(text) ? Image.FromFile(text) : this.picture2.InitialImage);
			text = Path.Combine(Product.FamilyImageLocation, "CableLay", "HY-Electrical.png");
			this.picture3.Image = (File.Exists(text) ? Image.FromFile(text) : this.picture3.InitialImage);
		}

		private void InitButtonImage()
		{
			this.buttonSee.Image = YArchitech.Electric.Resources.GetImage("ViewSee16");
			this.buttonLoad.Image = YArchitech.Electric.Resources.GetImage("ExcelLoad16");
			this.buttonDelete.Image = YArchitech.Electric.Resources.GetImage("Delete16");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.buttonSee, "图面查看");
			toolTip.SetToolTip(this.buttonLoad, "导入设备清册");
			toolTip.SetToolTip(this.buttonDelete, "删除");
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("EquipmentList");
			this.m_selectedFamily = (EquipmentFamily)Convert.ToInt32(xmlHelper.GetAttributeEx("Index", "0"));
			this.checkBoxIsAssignLevel.Checked = Convert.ToBoolean(xmlHelper.GetAttributeEx("IsAssignLevel", "false"));
			this.ReadFormDataFromXml(xmlHelper);
		}

		private void ReadFormDataFromXml(YArchitech.MEP.Electric.Common.XmlHelper xmlHelper)
		{
			if (xmlHelper == null)
			{
				xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			}
			EquipmentFamily selectedFamily = this.m_selectedFamily;
			if (selectedFamily == EquipmentFamily.Family_SwitchBox)
			{
				xmlHelper.SelectNode(string.Format("{0}\\{1}", "EquipmentList", "SwitchBox"));
				this.txtWidth.Text = xmlHelper.GetAttributeEx("Width", "800");
				this.txtHeight.Text = xmlHelper.GetAttributeEx("Height", "1600");
				this.txtDepth.Text = xmlHelper.GetAttributeEx("Depth", "600");
				return;
			}
			if (selectedFamily != EquipmentFamily.Family_Mechanical)
			{
				xmlHelper.SelectNode(string.Format("{0}\\{1}", "EquipmentList", "Electrical"));
				this.txtWidth.Text = xmlHelper.GetAttributeEx("Width", "600");
				this.txtHeight.Text = xmlHelper.GetAttributeEx("Height", "600");
				this.txtDepth.Text = xmlHelper.GetAttributeEx("Depth", "300");
				return;
			}
			xmlHelper.SelectNode(string.Format("{0}\\{1}", "EquipmentList", "Mechanical"));
			this.txtWidth.Text = xmlHelper.GetAttributeEx("Width", "600");
			this.txtHeight.Text = xmlHelper.GetAttributeEx("Height", "600");
			this.txtDepth.Text = xmlHelper.GetAttributeEx("Depth", "300");
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("EquipmentList");
			int selectedFamily = (int)this.m_selectedFamily;
			xmlHelper.SetAttribute("Index", selectedFamily.ToString());
			xmlHelper.SetAttribute("IsAssignLevel", this.IsAssignLevel.ToString());
			if (this.m_selectedFamily == EquipmentFamily.Family_SwitchBox)
			{
				xmlHelper.SelectNode(string.Format("{0}\\{1}", "EquipmentList", "SwitchBox"));
			}
			else if (EquipmentFamily.Family_Mechanical == this.m_selectedFamily)
			{
				xmlHelper.SelectNode(string.Format("{0}\\{1}", "EquipmentList", "Mechanical"));
			}
			else
			{
				xmlHelper.SelectNode(string.Format("{0}\\{1}", "EquipmentList", "Electrical"));
			}
			xmlHelper.SetAttribute("Width", this.txtWidth.Text.Trim());
			xmlHelper.SetAttribute("Height", this.txtHeight.Text.Trim());
			xmlHelper.SetAttribute("Depth", this.txtDepth.Text.Trim());
			xmlHelper.SaveXml("");
		}

		private void RefreshPictureBackColor()
		{
			this.picture1.BackColor = this.m_pictureBackColor;
			this.picture2.BackColor = this.m_pictureBackColor;
			this.picture3.BackColor = this.m_pictureBackColor;
			if (this.m_selectedFamily == EquipmentFamily.Family_SwitchBox)
			{
				this.picture1.BackColor = System.Drawing.Color.Red;
				return;
			}
			if (EquipmentFamily.Family_Mechanical == this.m_selectedFamily)
			{
				this.picture2.BackColor = System.Drawing.Color.Red;
				return;
			}
			this.picture3.BackColor = System.Drawing.Color.Red;
		}

		private void LoadDataFromDb()
		{
			if (string.IsNullOrWhiteSpace(CableLayDatabase.ThisDb.GetDataSource()))
			{
				this.txtProjectPath.Text = string.Empty;
				return;
			}
			this.txtProjectPath.Text = CableLayDatabase.ThisDb.GetDataSource();
			this.dataGridViewStart.Rows.Clear();
			this.dataGridViewEnd.Rows.Clear();
			foreach (EquipmentListData data in CableLayDatabase.ThisDb.GetEquipmentListDatas(EquipmentType.Equipment_Start))
			{
				this.NewDataGridViewRow(this.dataGridViewStart, data);
			}
			foreach (EquipmentListData data2 in CableLayDatabase.ThisDb.GetEquipmentListDatas(EquipmentType.Equipment_End))
			{
				this.NewDataGridViewRow(this.dataGridViewEnd, data2);
			}
			this.RefreshGridStateColumn();
		}

		private void SaveDataToDb()
		{
			if (string.IsNullOrWhiteSpace(CableLayDatabase.ThisDb.GetDataSource()) || !CableLayDatabase.ThisDb.IsOpening())
			{
				return;
			}
			this.dataGridViewStart.EndEdit();
			this.dataGridViewEnd.EndEdit();
			List<EquipmentListData> list = new List<EquipmentListData>();
			foreach (object obj in ((IEnumerable)this.dataGridViewStart.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				if (dataGridViewRow.Tag != null)
				{
					EquipmentListData item = (EquipmentListData)dataGridViewRow.Tag;
					list.Add(item);
				}
			}
			CableLayDatabase.ThisDb.SaveEquipmentListDatas(list, EquipmentType.Equipment_Start);
			List<EquipmentListData> list2 = new List<EquipmentListData>();
			foreach (object obj2 in ((IEnumerable)this.dataGridViewEnd.Rows))
			{
				DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
				if (dataGridViewRow2.Tag != null)
				{
					EquipmentListData item2 = (EquipmentListData)dataGridViewRow2.Tag;
					list2.Add(item2);
				}
			}
			CableLayDatabase.ThisDb.SaveEquipmentListDatas(list2, EquipmentType.Equipment_End);
		}

		public void NewDataGridViewRow(DataGridView grid, EquipmentListData data)
		{
			if (grid == null || data == null || string.IsNullOrWhiteSpace(data.Number))
			{
				return;
			}
			DataGridViewRow dataGridViewRow = new DataGridViewRow();
			int index = grid.Rows.Add(dataGridViewRow);
			dataGridViewRow = grid.Rows[index];
			dataGridViewRow.Tag = data;
			this.UpdateRowData(dataGridViewRow, false);
		}

		private void UpdateRowData(DataGridViewRow row, bool saveOrShow = false)
		{
			if (row == null || row.Tag == null)
			{
				return;
			}
			EquipmentListData equipmentListData = (EquipmentListData)row.Tag;
			if (saveOrShow)
			{
				equipmentListData.Level = Convert.ToDouble(row.Cells[3].Value);
				return;
			}
			row.Cells[0].Value = (equipmentListData.IsChecked ? YArchitech.Electric.Resources.GetImage("SuccessFace16") : YArchitech.Electric.Resources.GetImage("WaitingFace16"));
			row.Cells[1].Value = equipmentListData.Number;
			row.Cells[2].Value = equipmentListData.Name;
			row.Cells[3].Value = equipmentListData.Level;
		}

		private void RefreshGridStateColumn()
		{
			ICollection<Element> collection = CableLayRevitCommon.CollectElectricalEquipment(this.m_doc);
			if (collection == null || collection.Count == 0)
			{
				return;
			}
			this.RefreshGridStateColumn(this.dataGridViewStart, collection);
			this.RefreshGridStateColumn(this.dataGridViewEnd, collection);
		}

		private void RefreshGridStateColumn(DataGridView grid, ICollection<Element> elementSet)
		{
			if (grid == null || elementSet == null || elementSet.Count == 0)
			{
				return;
			}
			foreach (object obj in ((IEnumerable)grid.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				if (dataGridViewRow.Tag != null)
				{
					EquipmentListData equipmentListData = (EquipmentListData)dataGridViewRow.Tag;
					if (!equipmentListData.IsChecked)
					{
						foreach (Element element in elementSet)
						{
							Parameter parameter = element.GetParameter("编号");
							if (parameter != null && !(parameter.AsString() != equipmentListData.Number))
							{
								dataGridViewRow.Cells[0].Value = YArchitech.Electric.Resources.GetImage("SuccessFace16");
								equipmentListData.ObjectId = element.Id;
								equipmentListData.IsChecked = true;
								break;
							}
						}
					}
				}
			}
		}

		private void buttonBrowse_Click(object sender, EventArgs e)
		{
			string initialDirectory = Path.Combine(Product.InstallLocation, "UserWork");
			if (!string.IsNullOrWhiteSpace(this.txtProjectPath.Text.Trim()))
			{
				initialDirectory = Path.GetDirectoryName(this.txtProjectPath.Text.Trim());
			}
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "电缆敷设工程文件(*.hdb)|*.hdb";
			openFileDialog.InitialDirectory = initialDirectory;
			if (openFileDialog.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			CableLayDatabase.ThisDb.SetDataSource(openFileDialog.FileName);
			this.LoadDataFromDb();
		}

		private void buttonSee_Click(object sender, EventArgs e)
		{
			EquipmentListData equipmentListData = this.SelectedTagData();
			if (equipmentListData == null)
			{
				MessageBox.Show("请先选择一设备行！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			Element equipmentByNumber = CableLayRevitCommon.GetEquipmentByNumber(this.m_doc, equipmentListData.Number);
			if (equipmentByNumber == null)
			{
				MessageBox.Show("未在项目中找到指定编号的电气设备。", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			if (ViewUtils.ElementIsShowInView(this.m_doc, equipmentByNumber.Id, this.m_uiDoc.ActiveView.Id))
			{
				CableLayRevitCommon.HighlightElement(this.m_uiDoc, equipmentByNumber);
				return;
			}
			List<ElementId> viewsOpened = ViewUtils.GetViewsOpened(this.m_uiDoc, (CmdViewType)19);
			foreach (ElementId viewId in viewsOpened)
			{
				if (ViewUtils.ElementIsShowInView(this.m_doc, equipmentByNumber.Id, viewId))
				{
					CableLayRevitCommon.HighlightElement(this.m_uiDoc, equipmentByNumber);
					return;
				}
			}
			foreach (ElementId elementId in ViewUtils.GetViews(this.m_doc, (CmdViewType)3))
			{
				if (!viewsOpened.Contains(elementId) && ViewUtils.ElementIsShowInView(this.m_doc, equipmentByNumber.Id, elementId))
				{
                    Autodesk.Revit.DB.View view = this.m_doc.GetElement(elementId) as Autodesk.Revit.DB.View;
					if (view != null)
					{
						this.m_uiDoc.ActiveView = view;
						CableLayRevitCommon.HighlightElement(this.m_uiDoc, equipmentByNumber);
						return;
					}
				}
			}
			foreach (ElementId elementId2 in ViewUtils.GetViews(this.m_doc, CmdViewType.ThreeD))
			{
				if (!viewsOpened.Contains(elementId2) && ViewUtils.ElementIsShowInView(this.m_doc, equipmentByNumber.Id, elementId2))
				{
                    Autodesk.Revit.DB.View view2 = this.m_doc.GetElement(elementId2) as Autodesk.Revit.DB.View;
					if (view2 != null)
					{
						this.m_uiDoc.ActiveView = view2;
						CableLayRevitCommon.HighlightElement(this.m_uiDoc, equipmentByNumber);
						break;
					}
				}
			}
		}

		private void buttonLoad_Click(object sender, EventArgs e)
		{
			DataGridView grid = this.SelectedGrid();
			EquipmentListImportForm equipmentListImportForm = new EquipmentListImportForm();
			if (equipmentListImportForm.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			string excelFile = equipmentListImportForm.m_ExcelFile;
			int num = equipmentListImportForm.m_StartRow - 1;
            //new byte[1];
			int col = (int)(Encoding.ASCII.GetBytes(equipmentListImportForm.m_Attributes[EquipmentField.Field_Number])[0] - 65);
			int col2 = (int)(Encoding.ASCII.GetBytes(equipmentListImportForm.m_Attributes[EquipmentField.Field_Name])[0] - 65);
			int col3 = (int)(Encoding.ASCII.GetBytes(equipmentListImportForm.m_Attributes[EquipmentField.Field_Level])[0] - 65);
			Book book = new XmlBook();
			book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
			try
			{
				book.load(excelFile);
			}
			catch (System.Exception ex)
			{
				if ("libxl: Permission denied." == ex.Message)
				{
					MessageBox.Show("检测到此Excel文件已在外部打开，请先关闭，再重新操作。", "盈建科提示");
					return;
				}
			}
			Sheet sheet = book.getSheet(book.activeSheet());
			for (int i = num; i < sheet.lastRow(); i++)
			{
				EquipmentListData equipmentListData = new EquipmentListData();
				equipmentListData.Number = CableLayCommon.ReadExcelCell(sheet, i, col);
				equipmentListData.Name = CableLayCommon.ReadExcelCell(sheet, i, col2);
				string s = CableLayCommon.ReadExcelCell(sheet, i, col3);
				if (!string.IsNullOrWhiteSpace(equipmentListData.Number))
				{
					double level;
					if (double.TryParse(s, out level))
					{
						equipmentListData.Level = level;
					}
					DataGridViewRow dataGridViewRow = this.MatchGridData(grid, equipmentListData.Number);
					if (dataGridViewRow == null)
					{
						this.NewDataGridViewRow(grid, equipmentListData);
					}
					else
					{
						EquipmentListData equipmentListData2 = (EquipmentListData)dataGridViewRow.Tag;
						equipmentListData2.Name = equipmentListData.Name;
						equipmentListData2.Level = equipmentListData.Level;
						this.UpdateRowData(dataGridViewRow, false);
					}
				}
			}
			ICollection<Element> collection = CableLayRevitCommon.CollectElectricalEquipment(this.m_doc);
			if (collection == null || collection.Count == 0)
			{
				return;
			}
			this.RefreshGridStateColumn(grid, collection);
		}

		private void buttonDelete_Click(object sender, EventArgs e)
		{
			DataGridView dataGridView = this.SelectedGrid();
			if (dataGridView.SelectedRows.Count <= 0)
			{
				return;
			}
			int index = dataGridView.SelectedRows[0].Index;
			foreach (object obj in dataGridView.SelectedRows)
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				dataGridView.Rows.Remove(dataGridViewRow);
			}
			if (dataGridView.Rows.Count <= 0)
			{
				return;
			}
			dataGridView.ClearSelection();
			if (index >= dataGridView.Rows.Count)
			{
				dataGridView.Rows[0].Selected = true;
				return;
			}
			dataGridView.Rows[index].Selected = true;
		}

		private void buttonAssignment_Click(object sender, EventArgs e)
		{
			this.SelectedGrid().EndEdit();
			this.m_selectedGridRow = this.SelectedGridRow();
			if (this.m_selectedGridRow == null || this.m_selectedGridRow.Tag == null)
			{
				MessageBox.Show("请先选择一设备行！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			this.m_Handler.Request.Make(RequestId.Equipment_Assignment);
			this.m_ExEvent.Raise();
		}

		private void buttonLayout_Click(object sender, EventArgs e)
		{
			this.SelectedGrid().EndEdit();
			this.m_selectedGridRow = this.SelectedGridRow();
			if (this.m_selectedGridRow == null || this.m_selectedGridRow.Tag == null)
			{
				MessageBox.Show("请先选择一设备行！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			this.m_Handler.Request.Make(RequestId.Equipment_Layout);
			this.m_ExEvent.Raise();
		}

		private void picture1_Click(object sender, EventArgs e)
		{
			if (this.m_selectedFamily == EquipmentFamily.Family_SwitchBox)
			{
				return;
			}
			this.SaveFormDataToXml();
			this.m_selectedFamily = EquipmentFamily.Family_SwitchBox;
			this.RefreshPictureBackColor();
			this.ReadFormDataFromXml(null);
		}

		private void picture2_Click(object sender, EventArgs e)
		{
			if (EquipmentFamily.Family_Mechanical == this.m_selectedFamily)
			{
				return;
			}
			this.SaveFormDataToXml();
			this.m_selectedFamily = EquipmentFamily.Family_Mechanical;
			this.RefreshPictureBackColor();
			this.ReadFormDataFromXml(null);
		}

		private void picture3_Click(object sender, EventArgs e)
		{
			if (EquipmentFamily.Family_Electrical == this.m_selectedFamily)
			{
				return;
			}
			this.SaveFormDataToXml();
			this.m_selectedFamily = EquipmentFamily.Family_Electrical;
			this.RefreshPictureBackColor();
			this.ReadFormDataFromXml(null);
		}

		private void EquipmentListForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveFormDataToXml();
			this.SaveDataToDb();
		}

		private DataGridView SelectedGrid()
		{
			if (this.tabControl.SelectedIndex != 0)
			{
				return this.dataGridViewEnd;
			}
			return this.dataGridViewStart;
		}

		private EquipmentListData SelectedTagData()
		{
			DataGridView dataGridView = this.SelectedGrid();
			if (dataGridView.SelectedRows.Count <= 0)
			{
				return null;
			}
			DataGridViewRow dataGridViewRow = dataGridView.SelectedRows[0];
			if (dataGridViewRow.Tag == null)
			{
				return null;
			}
			return dataGridViewRow.Tag as EquipmentListData;
		}

		private DataGridViewRow SelectedGridRow()
		{
			DataGridView dataGridView = this.SelectedGrid();
			if (dataGridView.SelectedRows.Count <= 0)
			{
				return null;
			}
			return dataGridView.SelectedRows[0];
		}

		private DataGridViewRow MatchGridData(DataGridView grid, string number)
		{
			if (grid == null || string.IsNullOrWhiteSpace(number))
			{
				return null;
			}
			foreach (object obj in ((IEnumerable)grid.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				if (dataGridViewRow.Tag != null)
				{
					EquipmentListData equipmentListData = (EquipmentListData)dataGridViewRow.Tag;
					if (number == equipmentListData.Number)
					{
						return dataGridViewRow;
					}
				}
			}
			return null;
		}

		private void dataGridViewStart_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			DataGridView dataGridView = sender as DataGridView;
			if (dataGridView == null)
			{
				return;
			}
			if (e.RowIndex < 0 || e.RowIndex >= dataGridView.Rows.Count || e.ColumnIndex != 3)
			{
				e.Cancel = true;
				return;
			}
			this.m_valueOld = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim();
		}

		private void dataGridViewStart_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			DataGridView dataGridView = sender as DataGridView;
			if (dataGridView == null)
			{
				return;
			}
			if (e.RowIndex < 0 || e.RowIndex >= dataGridView.Rows.Count || e.ColumnIndex != 3)
			{
				return;
			}
			if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
			{
				dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.m_valueOld;
				return;
			}
			if (string.IsNullOrWhiteSpace(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim()))
			{
				dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.m_valueOld;
				return;
			}
			DataGridViewRow row = dataGridView.Rows[e.RowIndex];
			this.UpdateRowData(row, true);
		}

		private void dataGridViewStart_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			DataGridView dataGridView = sender as DataGridView;
			if (dataGridView == null)
			{
				return;
			}
			if (dataGridView.CurrentCell.OwningColumn.Index == 3)
			{
				e.Control.KeyPress -= this.dataGridView_TextBox_KeyPress;
				e.Control.KeyPress += this.dataGridView_TextBox_KeyPress;
				return;
			}
			e.Control.KeyPress -= this.dataGridView_TextBox_KeyPress;
		}

		private void dataGridView_TextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (StringTools.IsNumber(e.KeyChar))
			{
				return;
			}
			e.Handled = true;
		}

		private void dataGridViewStart_KeyDown(object sender, KeyEventArgs e)
		{
			this.dataGridView_KeyDown(sender, e);
		}

		private void dataGridViewEnd_KeyDown(object sender, KeyEventArgs e)
		{
			this.dataGridView_KeyDown(sender, e);
		}

		private void dataGridView_KeyDown(object sender, KeyEventArgs e)
		{
			try
			{
				Keys keyCode = e.KeyCode;
				if (keyCode == Keys.Delete)
				{
					this.buttonDelete_Click(null, null);
					e.SuppressKeyPress = true;
				}
			}
			catch (System.Exception)
			{
			}
		}

		private const string XmlNode_Root = "EquipmentList";

		private const string Att_Index = "Index";

		private const string Att_IsAssignLevel = "IsAssignLevel";

		private const string XmlNode_SwitchBox = "SwitchBox";

		private const string XmlNode_Mechanical = "Mechanical";

		private const string XmlNode_Electrical = "Electrical";

		private const string Att_Width = "Width";

		private const string Att_Height = "Height";

		private const string Att_Depth = "Depth";

		private RequestHandler m_Handler;

		private ExternalEvent m_ExEvent;

		private UIDocument m_uiDoc;

		private Document m_doc;

		private EquipmentFamily m_selectedFamily;

		private System.Drawing.Color m_pictureBackColor = System.Drawing.Color.Empty;

		private DataGridViewRow m_selectedGridRow;

		private string m_valueOld = string.Empty;
	}
}
